package com.huifaapp.mall.activity.Homefragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.ContentAdapter;
import com.huifaapp.mall.adapter.HelpcenterAdapter;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.ContentBean;
import com.huifaapp.mall.bean.HelpcenterBean;
import com.huifaapp.mall.uitils.NoScrollGridView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

public class HelpcenterActivity extends AppCompatActivity{
    private NoScrollGridView helpcenter_top;
    private RecyclerView helpcenter_content;
    private List<HelpcenterBean.DataBean>list;
    private HelpcenterAdapter helpcenteradapter;
    private ImageView iv_myhelp_back;
int currentItem=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helpcenter);
        initView();
    }

    private void initView() {
        helpcenter_top = (NoScrollGridView) findViewById(R.id.help_top);
        helpcenter_content = (RecyclerView) findViewById(R.id.help_bottom);
        iv_myhelp_back= (ImageView) findViewById(R.id.iv_myhelp_back);
        iv_myhelp_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


//        helpcenteradapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                //改变选中状态
//                helpcenteradapter.setCurrentItem(position);
//                //通知ListView改变状态
//                helpcenteradapter.notifyDataSetChanged();
//            }
//        });
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"users/helper/categorys")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        final HelpcenterBean help = gson.fromJson(response,HelpcenterBean.class);
                        list = help.getData();
                        Log.e("帮助中心",help.getData()+"帮助");
                        helpcenteradapter = new HelpcenterAdapter(HelpcenterActivity.this,help.getData(),currentItem);
                        helpcenter_top.setAdapter(helpcenteradapter);
                        reqItem(list.get(0).getCode());
                        helpcenter_top.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                reqItem(list.get(position).getCode());
                                currentItem=position;
                                //改变选中状态
                                helpcenteradapter.setCurrentItem(position);
                                helpcenteradapter = new HelpcenterAdapter(HelpcenterActivity.this,help.getData(),currentItem);
                                helpcenter_top.setAdapter(helpcenteradapter);
                            }
                        });

//
                    }
                });
        }


    private void reqItem(String code){
        OkHttpUtils.post().
                url(ApiManager.BASE_NIUPAI_URL+"users/helper/list")
                .addParams("code",code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson=new Gson();

                        ContentBean itemBean = gson.fromJson(response, ContentBean.class);
                        ContentAdapter contentAdapter=new ContentAdapter(itemBean.getData(),getApplicationContext());
                        helpcenter_content.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        helpcenter_content.setAdapter(contentAdapter);
                    }
                });
    }


}
